package routes

import (
	"net/http"

	"github.com/devops-smartbot/devops-smartbot/services/api-gateway/internal/handlers"
	"github.com/devops-smartbot/devops-smartbot/services/api-gateway/internal/middleware"
	"github.com/gin-gonic/gin"
)

// RegisterAPIRoutes attaches core API gateway endpoints.
func RegisterAPIRoutes(rg *gin.RouterGroup, alertHandler *handlers.AlertHandler, ticketHandler *handlers.TicketHandler, aiHandler *handlers.AIHandler, authHandler *handlers.AuthHandler, analyticsHandler *handlers.AnalyticsHandler, knowledgeHandler *handlers.KnowledgeHandler, settingsHandler *handlers.SettingsHandler, integrationHandler *handlers.IntegrationHandler, jwtSecret string) {
	rg.GET("/version", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"service": "api-gateway",
			"version": "v0.1.0",
		})
	})

	// Auth routes (public, no JWT required)
	auth := rg.Group("/auth")
	{
		auth.POST("/login", authHandler.Login)
		auth.POST("/register", authHandler.Register)
		auth.POST("/refresh", authHandler.RefreshToken)
		auth.POST("/logout", authHandler.Logout)
		// Protected route - requires JWT
		auth.GET("/profile", middleware.JWTMiddleware(jwtSecret), authHandler.GetProfile)
	}

	// Alert routes (currently public, can be protected later)
	alerts := rg.Group("/alerts")
	{
		alerts.GET("", alertHandler.ListAlerts)
		alerts.GET("/statistics", alertHandler.GetAlertStatistics)
		alerts.GET("/:id", alertHandler.GetAlert)
		alerts.POST("/:id/analyze", aiHandler.AnalyzeAlertById)
	}

	// Ticket routes (currently public, can be protected later)
	tickets := rg.Group("/tickets")
	{
		tickets.GET("", ticketHandler.ListTickets)
		tickets.GET("/statistics", ticketHandler.GetTicketStatistics)
		tickets.GET("/:id", ticketHandler.GetTicket)
		tickets.POST("", ticketHandler.CreateTicket)
		tickets.PATCH("/:id", ticketHandler.UpdateTicket)
		tickets.PUT("/:id", ticketHandler.UpdateTicket) // Also support PUT for frontend compatibility
		tickets.POST("/:id/comment", ticketHandler.AddComment)
		tickets.POST("/:id/analyze", aiHandler.AnalyzeTicketById)
		tickets.GET("/:id/analysis", aiHandler.GetAnalysisStatus)
	}

	// AI routes (currently public, can be protected later)
	ai := rg.Group("/ai")
	{
		ai.POST("/analyze", aiHandler.AnalyzeAlert)
	}

	// Analytics routes (currently public, can be protected later)
	analytics := rg.Group("/analytics")
	{
		analytics.GET("", analyticsHandler.GetAnalyticsData)
		analytics.GET("/alerts/trend", analyticsHandler.GetAlertTrend)
		analytics.GET("/tickets/trend", analyticsHandler.GetTicketTrend)
		analytics.GET("/performance", analyticsHandler.GetPerformanceMetrics)
	}

	// Knowledge base routes (currently public, can be protected later)
	knowledge := rg.Group("/knowledge")
	{
		knowledge.GET("", knowledgeHandler.ListDocs)
		knowledge.GET("/categories", knowledgeHandler.GetCategories)
		knowledge.GET("/popular", knowledgeHandler.GetPopular)
		knowledge.GET("/:id", knowledgeHandler.GetDoc)
		knowledge.POST("", knowledgeHandler.CreateDoc)
		knowledge.POST("/from-ticket", knowledgeHandler.CreateFromTicket)
		knowledge.PUT("/:id", knowledgeHandler.UpdateDoc)
		knowledge.DELETE("/:id", knowledgeHandler.DeleteDoc)
	}

	// Settings routes (currently public, can be protected later)
	settings := rg.Group("/settings")
	{
		// System settings
		settings.GET("/system", settingsHandler.GetSystemSettings)
		settings.PUT("/system", settingsHandler.UpdateSystemSettings)

		// Notification settings
		settings.GET("/notifications", settingsHandler.GetNotificationSettings)
		settings.PUT("/notifications", settingsHandler.UpdateNotificationSettings)

		// User preferences
		settings.GET("/preferences", settingsHandler.GetUserPreference)
		settings.PUT("/preferences", settingsHandler.UpdateUserPreference)

		// Integrations
		settings.GET("/integrations", settingsHandler.ListIntegrations)
		settings.GET("/integrations/:id", settingsHandler.GetIntegration)
		settings.POST("/integrations", settingsHandler.CreateIntegration)
		settings.PUT("/integrations/:id", settingsHandler.UpdateIntegration)
		settings.DELETE("/integrations/:id", settingsHandler.DeleteIntegration)
	}

	// Integration routes (if integration service is available)
	if integrationHandler != nil {
		integrations := rg.Group("/integrations")
		{
			// Generic
			integrations.GET("", integrationHandler.ListIntegrations)
			integrations.POST("/test", integrationHandler.TestIntegration)

			// Jira
			jira := integrations.Group("/jira")
			{
				jira.GET("/config", integrationHandler.GetJiraConfig)
				jira.PUT("/config", integrationHandler.UpdateJiraConfig)
				jira.POST("/sync-ticket", integrationHandler.SyncTicketToJira)
				jira.POST("/sync-issue", integrationHandler.SyncJiraIssue)
			}

			// PagerDuty
			pagerduty := integrations.Group("/pagerduty")
			{
				pagerduty.GET("/config", integrationHandler.GetPagerDutyConfig)
				pagerduty.PUT("/config", integrationHandler.UpdatePagerDutyConfig)
				pagerduty.POST("/incidents", integrationHandler.CreatePagerDutyIncident)
				pagerduty.GET("/incidents/:id", integrationHandler.GetPagerDutyIncident)
				pagerduty.PUT("/incidents/:id", integrationHandler.UpdatePagerDutyIncident)
			}
		}
	}
}
